<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use App\Models\Payment\RefundOrder;

class CreateRefundOrdersTable extends Migration
{

    const TABLE = RefundOrder::TABLE;

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        !Schema::hasTable(self::TABLE) &&
        Schema::create(self::TABLE, function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->string('refund_code', 100)->default('')->comment('退款编号');
            $table->string('paid_code', 100)->default('')->comment('支付单号');
            $table->unsignedInteger('refund_amount')->default(0)->comment('退款金额');
            $table->unsignedInteger('refund_balance_amount')->default(0)->comment('退款余额');
            $table->unsignedInteger('refund_point_amount')->default(0)->comment('退款积分抵扣金额');
            $table->unsignedInteger('refund_point')->default(0)->comment('退款积分');
            $table->dateTime('refunded_at')->nullable()->comment('退款时间');
            $table->string('refund_status', 50)->default('')->comment('退款状态');
            $table->uuid('parking_lot_id')->default('');
            $table->string('refund_type', 50)->default('');
            $table->string('error')->default('');
            $table->uuid('user_id')->default('');
            $table->string('user_name', 50)->default('');
            $table->string('remark')->default('');
            $table->uuid('related_id')->default('');
            $table->string('related_type')->default('');
            $table->uuid('payment_channel_id')->default('');
            $table->timestamps();
            $table->index(['related_id', 'related_type'], 'idx_related');
            $table->index(['parking_lot_id', 'related_id', 'paid_code']);
        });
        DB::statement("alter table `".self::TABLE."` comment '发起退款订单';");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists(self::TABLE);
    }
}
